﻿@page "/Financing"
@using AutoMapper;

@using Commonality;
@using ICarlifeRespository;
@using ICarlifeService;
@using Models;


@inject IMapper mapper;
@inject IBrandService BaseServer;
@inject IDistricjsService DistricjsService;
@inject IUOfWork UOfWork;
@inject ICarLeaseProjectService CarLeaseProjectService;

<h3 style="text-align:center">汽车融资租赁业务立项表</h3>
<div>
    <table class="table table-bordered tab-content">
        <tr>
            <td>项目来源</td>
            <td>
                <div>
                    <input type="radio" name="source" id="optionsRadios1"
                           value="1">客户介绍
                    <input type="radio" name="source" id="optionsRadios2"
                           value="2">朋友介绍

                </div>
                <div>
                    <input type="radio" name="source" id="optionsRadios3"
                           value="3">电话推广
                    <input type="radio" name="source" id="optionsRadios4"
                           value="4">其他
                </div>
                <div>
                    <input type="radio" name="source" id="optionsRadios5"
                           value="5">销售商介绍
                </div>
            </td>
        </tr>
    </table>

    <table class="table table-bordered" ShowLoadingInFirstRender="true" style="text-align:center">

        <tr>
            <td>品牌</td>
            <td>
                <div class="col-12 col-sm-6">
                    <Select Items="@brand" @bind-Value="@BindingModel.BrandId" OnSelectedItemChanged="OnCascadeBindSelectClick" />
                </div>
            </td>
            <td>系列</td>
            <td>
                <Select Items="@series" @bind-Value="@BindingModel.SeriesID" OnSelectedItemChanged="OnCascadeBindSelectClicks"></Select>
            </td>
            <td>型号</td>
            <td>
                <Select Items="@version" @bind-Value="@BindingModel.VersionID"></Select>
            </td>
        </tr>
        <tr>
            <td>颜色</td>
            <td>
                <BootstrapInput PlaceHolder=" " @bind-Value="@BindingModel.Color" />
            </td>
            <td>官方指导价（元）</td>
            <td>
                <BootstrapInput PlaceHolder=" " @bind-Value="@BindingModel.PuidePrice" />
            </td>
        </tr>
        <tr>
            <td>具体配置</td>
            <td>
                <BootstrapInput PlaceHolder="改装图请再此说明" @bind-Value="@BindingModel.SpecificConfiguration" />
            </td>
        </tr>
        <tr>
            <td>备注（需求时间、保险信息等）</td>
            <td>
                <Textarea placeholder="请输入 ..." rows="4" @bind-Value="@BindingModel.Remark"></Textarea>
            </td>
        </tr>
        <tr>
            <td>租赁类型</td>
            <td style="width:auto">
                <div class="col-12" width="300px">
                    <div style="float:left"></div>
                    <RadioList Items="@Lease" @bind-Value="@BindingModel.LeaseCategory" />
                </div>
            </td>
            <td>租赁数量</td>
            <td>
                <div class="col-12">
                    <BootstrapInput PlaceHolder=" " @bind-Value="@BindingModel.leaseNum" />台
                </div>
            </td>
        </tr>
        <tr>
            <td>上牌地</td>
            <td>
                <div class="col-12 col-sm-6">
                    <Select Items="@brands" @bind-Value="@BindingModel.ProvinceId" OnSelectedItemChanged="OnCascadeBindSelectClickas" />
                </div>
                <Select Items="@serieas" @bind-Value="@BindingModel.CityId"></Select>
            </td>
        </tr>
        <tr>
            <td>年限</td>
            <td style="width:auto">
                <div class="col-12">
                    <RadioList Items="@Year" @bind-Value="@BindingModel.Drivingexperience" />
                </div>
            </td>
        </tr>
        <tr>
            <td>先付比例</td>
            <td style="width:auto">
                <div class="col-12">
                    <RadioList Items="@Ratio" @bind-Value="@BindingModel.Localresidence" />
                </div>
            </td>
        </tr>
        <tr>
            <td>贷款金额</td>
            <td style="width:auto">
                <div class="col-12">
                    <RadioList Items="@Loan" @bind-Value="@BindingModel.Localdriving" />
                </div>
            </td>
        </tr>
        <tr>
            <td>客户名称</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Name" />
            </td>
            <td>立项日期</td>
            <td>
            </td>
        </tr>
        <tr>
            <td>手机</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.UserPhone" />
            </td>
            <td>性别</td>
            <td>
                <RadioList Items="@Sex" Value="@BindingModel.Sex" />
            </td>
        </tr>
        <tr>
            <td>身份证号码</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.IDCard" />
            </td>
            <td>婚姻状况</td>
            <td>
                <RadioList Items="@Marriage" @bind-Value="@BindingModel.MaritalStatus" />
            </td>
        </tr>
        <tr>
            <td>既往工作单位</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.PreviousEmployer" />
            </td>
            <td>工作时间</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.WorkTime" FormatString="yyyy-MM-dd" />
            </td>
        </tr>
        <tr>
            <td>部门</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Department" />
            </td>
            <td>职位</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Position" />
            </td>
        </tr>
        <tr>
            <td>现居住地</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.CurrentAddress" />
            </td>
        </tr>
        <tr>
            <td>驾龄情况</td>
            <td>
                <RadioList Items="@Driving" @bind-Value="@BindingModel.DrivingYears" />
            </td>
        </tr>
        <tr>
            <td>当地居住时间</td>
            <td>
                <RadioList Items="@LocationTime" @bind-Value="@BindingModel.CurrentDwellTime" />
            </td>
        </tr>
        <tr>
            <td>当地驾龄情况</td>
            <td>
                <RadioList Items="@LocalDrivingYears" @bind-Value="@BindingModel.CurrentDrivingYears" />
            </td>
        </tr>
        <tr>
            <td>配偶姓名</td>
            <td>
                <BootstrapInput PlaceHolder="如无，填写其他直系亲属" @bind-Value="@BindingModel.MateName" />
            </td>
            <td>配偶联系电话</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Re_Phone" />
            </td>
        </tr>
        <tr>
            <td>配偶身份证</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.UserLeaseID" />
            </td>
            <td>配偶收入</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Monthlyincome" />
            </td>

        </tr>
        <tr>
            <td>配偶工作单位</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Professional" />
            </td>
            <td>配偶担保</td>
            <td>
                <RadioList Items="@Security" @bind-Value="@BindingModel.Whetherornot" />
            </td>

        </tr>
        <tr>
            <td>配偶职务</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.job" />
            </td>
            <td>子女情况</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Situation" />
            </td>

        </tr>

        <tr>
            <td>家庭其他收入（元）</td>
            <td>
                <BootstrapInput PlaceHolder="" @bind-Value="@BindingModel.Income" />
            </td>
        </tr>
        <tr>
            <td>房屋类型</td>
            <td>
                <RadioList Items="@House" @bind-Value="@BindingModel.Tenement" />
            </td>
        </tr>
        <tr>
            <td>其他描述</td>
            <td>
                <Textarea placeholder="请输入 ..." rows="4" />
            </td>
        </tr>
        <tr>
            <td>联系人姓名</td>
            <td>关系</td>
            <td>
                联系方式
            </td>
            <td>
                <Button Color="Color.Secondary">+新增</Button>
            </td>
        </tr>
    </table>
    <div>
        <h4>
            附件：审核资料
            <LinkButton Text="链接按钮" Url="/FinancingFile" @bind-value=BindingModel.ChannelId />
        </h4>
    </div>
    <Button ButtonStyle="ButtonStyle.Round" Color="Color.Primary" OnClick="AddProjectApproval">保存</Button>
    <Button ButtonStyle="ButtonStyle.Round" Color="Color.Secondary">取消</Button>
</div>


@code {
    private IEnumerable<SelectedItem> brand { get; set; }


    private IEnumerable<SelectedItem> serieas { get; set; }
    private IEnumerable<SelectedItem> version { get; set; }
    private IEnumerable<SelectedItem> brands { get; set; }
    private IEnumerable<SelectedItem> series { get; set; }
    private CarLeaseProjectDTO BindingModel { get; set; } = new CarLeaseProjectDTO();

    private IEnumerable<SelectedItem>? Lease { get; set; }
    private IEnumerable<SelectedItem>? Year { get; set; }
    private IEnumerable<SelectedItem>? Ratio { get; set; }
    private IEnumerable<SelectedItem>? Loan { get; set; }
    private IEnumerable<SelectedItem>? Sex { get; set; }
    private IEnumerable<SelectedItem>? Marriage { get; set; }
    private IEnumerable<SelectedItem>? Driving { get; set; }
    private IEnumerable<SelectedItem>? LocationTime { get; set; }
    private IEnumerable<SelectedItem>? LocalDrivingYears { get; set; }
    private IEnumerable<SelectedItem>? Security { get; set; }
    private IEnumerable<SelectedItem>? House { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();//初始化之后

        brand = mapper.Map<IEnumerable<TreeBindDTO>, IEnumerable<SelectedItem>>((BaseServer.BindTree(0)).AsEnumerable());
        brands = mapper.Map<IEnumerable<TreeBindDTO>, IEnumerable<SelectedItem>>((await DistricjsService.BindTrees(0)).AsEnumerable());

        Lease = new List<SelectedItem>
        {
            new SelectedItem("融租", "融租"),
            new SelectedItem("营租", "营租")
        };
        Year = new List<SelectedItem>
        {
            new SelectedItem("1年", "1年"),
            new SelectedItem("2年", "2年"),
            new SelectedItem("3年", "3年"),
            new SelectedItem("4年", "4年")
        };
        Ratio = new List<SelectedItem>
        {
            new SelectedItem("10%以下", "10%以下"),
            new SelectedItem("10（含）%-25%", "10（含）%-25%"),
            new SelectedItem("25（含）%-30%", "25（含）%-30%"),
            new SelectedItem("30%以上", "30%以上")
        };
        Loan = new List<SelectedItem>
        {
            new SelectedItem("6.5万(含)以下", "6.5万(含)以下"),
            new SelectedItem("6.5万-20(含)万", "6.5万-20(含)万"),
            new SelectedItem("20万以上", "20万以上")
        };
        Sex = new List<SelectedItem>
        {
            new SelectedItem("男", "男"),
            new SelectedItem("女", "女"),
        };
        Marriage = new List<SelectedItem>
        {
            new SelectedItem("已婚", "已婚"),
            new SelectedItem("未婚", "未婚"),
        };
        Driving = new List<SelectedItem>
        {
            new SelectedItem("2年以下", "2年以下"),
            new SelectedItem("2-3年", "2-3年"),
            new SelectedItem("3-5年", "3-5年"),
            new SelectedItem("5年以上", "5年以上")
        };
        LocationTime = new List<SelectedItem>
        {
            new SelectedItem("2年以下", "2年以下"),
            new SelectedItem("2-3年", "2-3年"),
            new SelectedItem("3-5年", "3-5年"),
            new SelectedItem("5年以上", "5年以上")
        };
        LocalDrivingYears = new List<SelectedItem>
        {
            new SelectedItem("2年以下", "2年以下"),
            new SelectedItem("2-3年", "2-3年"),
            new SelectedItem("3-5年", "3-5年"),
            new SelectedItem("5年以上", "5年以上")
        };
        Security = new List<SelectedItem>
        {
            new SelectedItem("是", "是"),
            new SelectedItem("否", "否")
        };
        House = new List<SelectedItem>
        {
            new SelectedItem("自置（无按揭）", "自置（无按揭）"),
            new SelectedItem("自置（有按揭，租住，其他）", "自置（有按揭，租住，其他）"),
            new SelectedItem("租住", "租住"),
            new SelectedItem("其他", "其他")
        };
    }
    //系列显示
    private async Task OnCascadeBindSelectClick(SelectedItem item)
    {
        //模拟异步通讯切换线程
        await Task.Delay(10);
        series = mapper.Map<IEnumerable<TreeBindDTO>, IEnumerable<SelectedItem>>((BaseServer.BindTree(Convert.ToInt32(item.Value))).AsEnumerable());
        BindingModel.BrandId = Convert.ToInt32(item.Value);
        StateHasChanged();//状态已更改
    }
    //型号显示
    private async Task OnCascadeBindSelectClicks(SelectedItem item)
    {
        //模拟异步通讯切换线程
        await Task.Delay(10);
        version = mapper.Map<IEnumerable<TreeBindDTO>, IEnumerable<SelectedItem>>((BaseServer.BindTree(Convert.ToInt32(item.Value))).AsEnumerable());
        BindingModel.SeriesID = Convert.ToInt32(item.Value);
        StateHasChanged();//状态已更改
    }
    private async Task OnCascadeBindSelectClickas(SelectedItem item)
    {
        //模拟异步通讯切换线程
        await Task.Delay(10);
        serieas = mapper.Map<IEnumerable<TreeBindDTO>, IEnumerable<SelectedItem>>((await DistricjsService.BindTrees(Convert.ToInt32(item.Value))).AsEnumerable());
        BindingModel.ProvinceId = Convert.ToInt32(item.Value);
        StateHasChanged();//状态已更改
    }

    //保存
    public async Task<ResultReposeDTO> AddProjectApproval()
    {
        ResultReposeDTO result = new();
        using (var tarn = UOfWork.dbcontext().Database.BeginTransaction())//开启事务
        {
            try
            {
                await CarLeaseProjectService.Insert(mapper.Map<CarLeaseProject>(BindingModel));


                //成功提交
                tarn.Commit();
                return result;
            }
            catch (Exception)
            {
                //失败回滚
                tarn.Rollback();

                result.code = ResponseResultCode.Failed;
                return result;
            }
        }
    }
}
